<template>
    <div class="content">
        <div :style="{ backgroundImage: 'url(' + item.image + ')' }" class="food_image">
            <div class="foodName image_text">{{ item.name }}</div>
            <div class="aliasName marginTop image_text"> {{ item.alias }}</div>
        </div>
        <div class="originSelect marginTop">
            <div class="title">说明:</div>
            <div>{{ item.main_text }}</div>
        </div>
        <div class="originSelect marginTop">
            <div class="title">使用食材:</div>
            <div class="ingredient_list">
                <div
                    v-for="item in ingredients"
                    :key="item.id"
                    class="ingredient_item"
                    :style="{ backgroundImage: 'url(' + item.image + ')' }"
                >
                    <div class="ingredient_name">{{ item.name }}</div>
                </div>
            </div>
        </div>
        <div class="originSelect marginTop">
            <div class="title">营养成分:</div>
            <div id="element_list"></div>
        </div>
        <div class="originSelect marginTop">
            <div class="title">烹饪方法:</div>
            <div>{{ item.production_method }}</div>
        </div>
    </div>
</template>
<script setup>
import { getCookById, queryAllFood, queryAllElementList } from './api.js'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router';
import * as echarts from 'echarts'
const route = useRoute()
const item = ref({})
const elementData = ref([])
const ingredients = ref([])
const elements = ref([])

onMounted(() => {
    const id = route.query.id
    Promise.all([getCookById(id), queryAllFood(), queryAllElementList()]).then(res => {
        item.value = res[0].result
        const ingredientData = res[1].result
        elementData.value = res[2].result
        countIngredient(ingredientData)
    })
})

const countIngredient = (data) => {
    const ingredientList = item.value.ingredients ? JSON.parse(item.value.ingredients) : []
    if (ingredientList) {
        ingredientList.forEach(item => {
            const ingredient = data.find(i => i.id === item.id)
            if (ingredient) {
                ingredients.value.push({
                    ...ingredient,
                    weight: item.weight
                })
            }
        })
    }
    countElement(elementData.value, ingredients.value || [])
}

const countElement = (elData, ingredients) => {
    const list = []
    ingredients.forEach(ing => {
        const { weight, nutrition_list } = ing
        const elList = nutrition_list ? JSON.parse(nutrition_list) : []
        console.log(elList, '==elList=========>')
        elList.forEach(el => {
            const element = elData.find(e => e.id === el.id)
            if (element) {
                if (!list.find(curr => curr.id === element.id)) {
                    list.push({
                        ...element,
                        weight: Number(weight) * Number(el.weight) / 100
                    })
                } else {
                    const current = list.find(e => e.name === element.name)
                    current.weight = Number(current.weight) + (Number(weight) * Number(el.weight) / 100)
                }
            }
        })
    })
    elements.value = list
    initElementChart()
}

const initElementChart = () => {
    const data = elements.value.map(item => {
        return {
            name: item.name,
            value: item.weight
        }
    })
    const myChart = echarts.init(document.getElementById('element_list'))
    const option = {
        tooltip: {
            trigger: 'item'
        },
        label: {
            show: true,
            formatter: '{b}\n{c}'
        },
        series: [
            {
                type: 'pie',
                radius: '50%',
                data: data,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    }
    myChart.setOption(option)
}

</script>

  
<style lang="scss" scoped>
img {
    display: block;
    width: 300px;
    height: auto;
    object-fit: contain;
    /* Change from 'cover' to 'contain' */
    border-radius: 16px;
    z-index: 1;
}

#element_list {
    width: 100%;
    height: 300px;
}

::v-deep .food_image{
    width: 100%;
    height: 250px;
    background-size: cover;
    background-position: center;
    position: relative;

    .foodName {
        margin-top: 40px;
        font-size: 30px;
        width: 100% !important;
        color: rgb(0, 0, 0, 0.5);
    }

    .aliasName {
        position: absolute;
        left: 20px;
        top: 60px;
        font-size: 15px;
        color: rgb(0, 0, 0, 0.5);
    }
    .image_text {
        background-color: white;
        width: auto;
        display: inline-block;
    }
}

::v-deep .ingredient_list {
    display: flex;
    flex-wrap: wrap;
    .ingredient_item {
        width: 100px;
        height: 100px;
        background-size: cover;
        background-position: center;
        margin-right: 10px;
        margin-bottom: 10px;
        border-radius: 10px;
        position: relative;
        border: 1px solid saddlebrown;
        .ingredient_name {
            width: 100%;
            text-align: center;
            font-size: 12px;
            position: absolute;
            bottom: 0;
            background: white;
            border-bottom-left-radius: 10px;
            border-bottom-right-radius: 10px;
        }
    }
}

.marginTop {
    margin-top: 50px;
    line-height: 20px;
}


.textContainer {
    position: absolute;
    top: 80px;
    left: 80px;
    padding: 20px 15px;
    z-index: 1;
}

.content {
    position: relative;
    padding: 20px 15px;
    text-align: left;
    font-size: 14px;
    letter-spacing: 2px;
}

.title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 20px;
}
</style>